package com.nbsaas.boot.controller.app;

import com.nbsaas.boot.app.api.apis.AppRoleApi;
import com.nbsaas.boot.app.api.domain.enums.RoleType;
import com.nbsaas.boot.app.api.domain.request.AppRoleRequest;
import com.nbsaas.boot.app.api.domain.request.AppRoleSearch;
import com.nbsaas.boot.app.api.domain.response.AppRoleResponse;
import com.nbsaas.boot.app.api.domain.simple.AppRoleSimple;
import com.nbsaas.boot.app.ext.apis.AppMenuExtApi;
import com.nbsaas.boot.app.ext.apis.AppUserRoleExtApi;
import com.nbsaas.boot.app.ext.domain.request.MenuDragRequest;
import com.nbsaas.boot.app.ext.domain.request.UpdateRoleMenuRequest;
import com.nbsaas.boot.app.ext.domain.simple.AppMenuExtSimple;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.request.RequestId;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequiresAuthentication
@RestController
@RequestMapping("/appRole")
public class AppRoleController {

    @Resource
    private AppRoleApi appRoleApi;

    @RequestMapping("/create")
    public ResponseObject<AppRoleResponse> create(@RequestBody AppRoleRequest request) {
        request.setTenant(UserUtils.user().getShop());
        request.setRoleType(RoleType.shop);
        return appRoleApi.create(request);
    }

    @RequestMapping("/update")
    public ResponseObject<AppRoleResponse> update(@RequestBody  AppRoleRequest request) {
        request.setTenant(UserUtils.user().getShop());
        return appRoleApi.update(request);
    }

    @RequestMapping("/delete")
    public ResponseObject<?> delete(@RequestBody AppRoleRequest request) {
        request.setTenant(UserUtils.user().getShop());
        request.setRoleType(RoleType.shop);
        return appRoleApi.delete(request);
    }



    @RequestMapping("/view")
    public ResponseObject<?> viewByOne(@RequestBody AppRoleSearch request) {
        request.setTenant(UserUtils.user().getShop());
        request.setRoleType(RoleType.shop);
        return appRoleApi.viewByOne(request);
    }

    @RequestMapping("/search")
    public PageResponse<AppRoleSimple> search(@RequestBody AppRoleSearch request) {
        request.setTenant(UserUtils.user().getShop());
        request.setRoleType(RoleType.shop);
        return appRoleApi.search(request);
    }




    @RequestMapping("/root")
    public ListResponse<AppMenuExtSimple> root() {
        return appMenuExtApi.root(UserUtils.user().getAppId());
    }

    @RequestMapping("/findByRole")
    public ListResponse<AppMenuExtSimple> findByRole(Long appId, Long role) {
        return appMenuExtApi.findByRole(appId, role);
    }

    @RequestMapping("/selectForPermission")
    public ListResponse<Long> selectForPermission( Long role) {
        return appMenuExtApi.selectForPermission(UserUtils.user().getAppId(), role);
    }

    @RequestMapping("/permissions")
    public List<String> permissions( Long role) {
        return appMenuExtApi.permissions(UserUtils.user().getAppId(), role);
    }

    @RequestMapping("/updateRoleMenus")
    public ResponseObject<?> updateRoleMenus(@RequestBody UpdateRoleMenuRequest request) {
        return appMenuExtApi.updateRoleMenus(request);
    }

    @Resource
    private AppMenuExtApi appMenuExtApi;
}
